<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="showPermission=true,title='新增权限点'">添加权限</el-button>
      <el-table :data="list" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" size="mini" type="text" @click="addPermission(row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="showPermission"
      width="30%"
      :before-close="onCancel"
    >
      <el-form ref="permissionForm" :rules="rules" :model="permissionForm" label-width="120px" style="margin-right: 50px;">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permissionForm.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permissionForm.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="permissionForm.description" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="permissionForm.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
      </el-form>
      <span slot="footer" style="display: flex;justify-content: center;margin-bottom: 10px;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="onCancel">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addPermissionList, getPermissionList, editPermissionList, detailsPermissionList, delPermissionList } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      title: '新增权限点',
      list: [],
      showPermission: false,
      permissionForm: {
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: 0,
        enVisible: 0
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            // 自定义校验模式
            validator: async(rule, value, callback) => {
              const result = await getPermissionList()
              // 编辑和新增的表单验证一起验证
              if (result.data.some(v => v.name === value && v.id !== this.permissionForm.id)) {
                callback(new Error('权限中已经有该名称了'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            // 自定义校验模式
            validator: async(rule, value, callback) => {
              const result = await getPermissionList()
              // 编辑和新增的表单验证一起验证
              if (result.data.some(v => v.code === value && v.id !== this.permissionForm.id)) {
                callback(new Error('权限中已经有该标识了'))
              } else {
                callback()
              }
            }
          }
        ],
        description: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const { data } = await getPermissionList()
      // console.log(data)
      this.list = transListToTreeData(data, 0)
    },
    // 点击提交
    async submitForm() {
      await this.$refs.permissionForm.validate()
      // 编辑
      if (this.permissionForm.id) {
        await editPermissionList(this.permissionForm)
        this.$message.success('编辑成功')
        delete this.permissionForm.id
      } else {
        // 新增
        await addPermissionList(this.permissionForm)
        this.$message.success('新增成功')
        this.permissionForm.pid = 0
        this.permissionForm.type = 1
      }
      this.$refs.permissionForm.resetFields()
      this.showPermission = false
      this.getList()
    },
    // 点击添加
    addPermission(id) {
      this.title = '新增权限点'
      this.showPermission = true
      this.permissionForm.pid = id
      this.permissionForm.type = 2
    },
    // 点击取消
    onCancel() {
      this.$refs.permissionForm.resetFields()
      this.showPermission = false
    },
    // 编辑数据回填
    async editPermission(id) {
      this.showPermission = true
      this.title = '编辑权限点'
      const { data } = await detailsPermissionList(id)
      this.permissionForm = data
      this.permissionForm.id = id
    },
    // 删除
    delPermission(id) {
      this.$confirm('您确认要删除该权限吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        delPermissionList(id)
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {})
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
